package com.china08.yxyapi.api.manage.stu.mystudy.mistakes;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.mg.mistakes.StuMistakesQues;
import com.china08.yxyapi.entity.mg.mistakes.StuMistakesQuesWeike;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.repository.mg.mistakes.StuMistakesQuesRepository;
import com.china08.yxyapi.repository.mg.mistakes.StuMistakesQuesWeikeRepository;
import com.china08.yxyapi.service.res.mistakes.impl.MistakesWeikeServiceImpl;
import com.china08.yxyapi.util.YxyPlayUrlForWeiKechengBuilder;
import com.china08.yxyapi.util.YxyResUtil;

/**
 * 学生端查看错题讲解的微课
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class StuMistakesQuesWeikeController extends YxyApiBaseController {

    @Autowired
    YxyResResourceRepository yxyResResourceRepository;

    @Autowired
    StuMistakesQuesRepository stuMistakesQuesRepository;

    @Autowired
    MistakesWeikeServiceImpl mistakesWeikeServiceImpl;

    // @Autowired
    // IStuVipValidateService stuVipValidateServicel;

    @Autowired
    YxyResUtil yxyResUtil;

    @Autowired
    private YxyPlayUrlForWeiKechengBuilder yxyPlayUrlForWeiKechengBuilder;

    // @Autowired
    // private StuVipWeikeViewServiceImpl stuVipWeikeViewServiceImpl;

    @Autowired
    private StuMistakesQuesWeikeRepository stuMistakesQuesWeikeRepository;

    @RequestMapping(value = "/api_yw/manage/stu/mystudy/mistake/weike", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel model) {

        StuMistakesQues smq = stuMistakesQuesRepository.findById(model.id);
        RespModel respModels = new RespModel();
        if (smq == null) {
            respModels.mistakesWeikeLst = null;
            return respModels;
        }

        // 获取学生id, 如果没有传递此参数,使用当前登录人id
        String stuId = StringUtils.isNotBlank(model.stuId) ? model.stuId : getProfile(request, response).getId();

        String schId = getProfile(request, response).getSchoolId();

        List<YxyResResource> wkIdLst = mistakesWeikeServiceImpl.getMistakesWeiKeForStudent(smq.getPublishUserId(),
                schId, smq.getKnowIdLst());
        if (wkIdLst == null) {
            respModels.mistakesWeikeLst = null;
            return respModels;
        }
        StuMistakesQuesWeike sqw = stuMistakesQuesWeikeRepository.findById(stuId);
        List<MistakesWeike> returnLst = new ArrayList<>();
        for (YxyResResource yxyResResource : wkIdLst) {
            MistakesWeike mistakesWeike = new MistakesWeike();

            // 个人vip
            String vipString = getProfile(request, response).getUserVip();
            Boolean userVip;
            if (vipString.equals("true")) {
                userVip = true;
                respModels.vip = 100;
            } else {
                userVip = false;

                if (sqw == null) {
                    respModels.vip = 3;
                    yxyPlayUrlForWeiKechengBuilder.setFreeTimes(3);
                } else {
                    respModels.vip = sqw.getTimes();
                    yxyPlayUrlForWeiKechengBuilder.setFreeTimes(sqw.getTimes());
                }

            }

            // 资源vip
            String resVipString = getProfile(request, response).getUserVip();
            Boolean resVip;
            if (resVipString.equals("true")) {
                resVip = true;
            } else {
                resVip = false;
            }
            // 播放url
            // String url =
            // yxyWeiKeChengUtil.genWeiKechengPlayUrl(yxyResResource.getId(),
            // stuId, userVip,
            // stuVipWeikeViewServiceImpl.stuVipValidate(stuId));
            yxyPlayUrlForWeiKechengBuilder.setUserId(stuId);
            yxyPlayUrlForWeiKechengBuilder.setResourceId(yxyResResource.getId());
            yxyPlayUrlForWeiKechengBuilder.setUserVip(userVip);
            yxyPlayUrlForWeiKechengBuilder.setResVip(resVip);

            yxyPlayUrlForWeiKechengBuilder.setViewFrom("mistakes");
            String build = yxyPlayUrlForWeiKechengBuilder.build();

            mistakesWeike.url = build;
            mistakesWeike.cover = yxyResResource.getCover();
            mistakesWeike.showTitle = yxyResResource.getTitle();
            returnLst.add(mistakesWeike);
            respModels.mistakesWeikeLst = returnLst;
        }
        // 增加学生的vip验证的, 100(为100), 其它数值为可以免费查看的次数. 2017.07.19 Richard.Wang
        // respModels.vip = stuVipValidateServicel.stuVipValidate(stuId, schId);

        return respModels;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiReqModel {
        public String stuId;
        // id
        public String id;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {
        public List<MistakesWeike> mistakesWeikeLst;
        public Integer vip;// 当返回值为100,表明为vip, 如果不是100,返回可以查看次数
    }

    protected static class MistakesWeike {

        // 微课视频链接地址
        public String url;

        // 展示标题（区分老师与微课）
        public String showTitle;

        // 视频封面
        public String cover;
    }
}
